package cqrtplm.service.impl;

import com.hustcad.plm.pdm.user.model.dto.PdmUserDTO;
import com.hustcad.plm.pdm.workflow.model.entity.FlowTaskRecord;
import com.hustcad.plm.pdm.workflow.model.entity.UserTaskDTO;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerViewService;
import cqrtplm.service.CountersignSynergyApproverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : pieStar
 * @create 2025/7/18 22:44
 */
@Slf4j
@Service
public class CountersignSynergyApproverServiceImpl implements CountersignSynergyApproverService {


    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;

    @Resource
    private TyplmWorkFlowContainerViewService typlmWorkFlowContainerViewService;


    @Override
    public void setTheSynergyApprover(BigInteger workFlowContainerId) {
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(workFlowContainerId);
        WorkFlowContainerView reWorkFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                workFlowContainerId);
        // 会签(越南SQE）
        setNextNodeUserList(reWorkFlowContainerView, "Activity_04r6m3t", "Activity_1b1pk5t");
        // 会签(通机工艺判断）
        setNextNodeUserList(reWorkFlowContainerView, "Activity_0y5pml1", "Activity_121ld45");
        // 会签(OPE工艺判断）
        setNextNodeUserList(reWorkFlowContainerView, "Activity_0s63bzk", "Activity_121ld45");
        // 会签(重庆SQE)
        setNextNodeUserList(reWorkFlowContainerView, "Activity_0gxig9q", "Activity_0tnly6v");

        typlmWorkFlowContainerViewService.update(reWorkFlowContainerView);
    }

    /**
     * 设置下一个节点的用户列表
     *
     * @param workFlowContainerView 工作流容器视图对象
     */
    private void setNextNodeUserList(WorkFlowContainerView workFlowContainerView, String currentNodeId,
                                     String nextNodeId) {
        List<UserTaskDTO> userTaskList = workFlowContainerView.getUserTaskList();
        UserTaskDTO countersign = userTaskList.stream()
                .filter(u -> u != null && u.getId() != null && u.getId().equals(currentNodeId))
                .findFirst().orElse(null);

        UserTaskDTO countersignNext = userTaskList.stream()
                .filter(u -> u != null && u.getId() != null && u.getId().equals(nextNodeId))
                .findFirst().orElse(null);
        if (countersign != null) {
            List<PdmUserDTO> userList = countersign.getUserList();

            List<FlowTaskRecord> flowTaskRecordList = workFlowContainerView.getFlowTaskRecordList();
            FlowTaskRecord flowTaskRecord = flowTaskRecordList.stream().filter(f -> f.getTaskDefId() != null && f.getTaskDefId().equals("Activity_04r6m3t")).findFirst().orElse(null);
            // 检查 flowTaskRecord 是否为 null
            BigInteger operatorOid = flowTaskRecord != null ? flowTaskRecord.getOperatorOid() : null;
            // 从 userList 中找出 id 等于 operatorOid 的对象
            List<PdmUserDTO> collect = userList.stream()
                    .filter(user -> user.getOid() != null && operatorOid != null && user.getOid().equals(operatorOid.toString()))
                    .collect(Collectors.toList());
            // 检查 countersignNext 是否为 null
            if (countersignNext != null) {
                countersignNext.setUserList(collect);
            }
        }
    }
}
